package code;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class ServerThread implements Runnable{

    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    private int count = 1;
    @Override
    public void run() {
        String ip = socket.getInetAddress().getHostAddress();
        File file = new File("E:\\" + ip + ".jpg");
        if (file.exists()) {
            file = new File("E:\\" + ip + "_" + count + ".jpg");

        }


        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            BufferedWriter result = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        InputStream inputStream = null;
        try {
            inputStream = socket.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        int b = 0;

        while (true) {
            try {
                if (!((b = inputStream.read()) != -1)) break;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                outputStream.write(b);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }


    }
}
